package com.sjd.mall.service.goods.impl;

import com.alibaba.fastjson.JSONObject;
import com.sjd.common.core.domain.AjaxResult;
import com.sjd.common.utils.StringUtils;
import com.sjd.mall.domain.SjdGoodsCommentDO;
import com.sjd.mall.domain.dto.goods.*;
import com.sjd.mall.mapper.SjdGoodsCommentMapper;
import com.sjd.mall.service.goods.GoodsCommentService;
import com.sjd.wx.utils.ThirdSessionHolder;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author : Mr.s
 * @version : 1.0
 * @description : 商品评价实现类
 * @date : 2022-01-17 15:42
 */
@Service
public class GoodsCommentServiceImpl implements GoodsCommentService {
    @Autowired
    private SjdGoodsCommentMapper commentMapper;

    /**
     * 添加评论
     *
     * @param requestDTO requestDTO
     * @return return
     */
    @Override
    public AjaxResult addComment(SjdGoodsCommentAddRequestDTO requestDTO) {
        Long goodsId = requestDTO.getGoodsId();
        Long sjdMemberId = ThirdSessionHolder.getSjdMemberId();
        String photosStr = String.join(",", requestDTO.getPhotoUrls());
        SjdGoodsCommentDO sjdGoodsCommentDO = SjdGoodsCommentDO.builder()
                .goodsId(goodsId)
                .parentId(requestDTO.getParentCommentId())
                .userId(sjdMemberId)
                .commentStr(requestDTO.getCommentString())
                .commentPicture(photosStr)
                .commentLevel(requestDTO.getCommentLevel())
                .build();
        System.out.println(JSONObject.toJSONString(sjdGoodsCommentDO));
        commentMapper.insertSelective(sjdGoodsCommentDO);
        return AjaxResult.success();
    }

    /**
     * 查询评论   小程序
     *
     * @param requestDTO requestDTO
     * @return return
     */
    @Override
    public List<SjdGoodsCommentQueryResponseDTO> queryCommentApp(SjdGoodsCommentQueryRequestDTO requestDTO) {
        //主评论
        List<SjdGoodsCommentQueryResponseDTO> rootCommentList = commentMapper.getRootCommentApp(requestDTO.getGoodsId());
        if (CollectionUtils.isEmpty(rootCommentList)) {
            return new ArrayList<>();
        }
        //根据主评论 获取追加评论
        Iterator<SjdGoodsCommentQueryResponseDTO> iterator = rootCommentList.iterator();
        while (iterator.hasNext()) {
            SjdGoodsCommentQueryResponseDTO rootComment = iterator.next();
            if (rootComment == null) {
                iterator.remove();
                continue;
            }
            String commentPhoto = rootComment.getCommentPhoto();
            if (StringUtils.isNotBlank(commentPhoto)) {
                rootComment.setCommentPicture(commentPhoto);
            }
            List<SjdGoodsCommentQueryResponseDTO> childComment = commentMapper.getChildCommentApp(rootComment.getGoodsCommentId());
            rootComment.setChildComments(childComment);


        }
        return rootCommentList;
    }

    /**
     * 模糊查找   管理平台 不限制主评论/追加评论
     *
     * @param requestDTO requestDTO
     * @return return
     */
    @Override
    public List<SjdWebSiteGoodsCommentQueryResponseDTO> queryCommentWebsite(SjdWebSiteGoodsCommentQueryRequestDTO requestDTO) {
        return commentMapper.getCommentWebSite(null, requestDTO.getMemberNickName(), requestDTO.getGoodsName(), requestDTO.getStartTime(), requestDTO.getEndTime());
    }

    /**
     * 根据商品id 查询
     *
     * @param goodsId goodsId
     * @return return
     */
    @Override
    public List<SjdWebSiteGoodsCommentQueryResponseDTO> queryCommentWebsiteByGoodsId(Long goodsId) {
        return commentMapper.getCommentWebSiteByGoodsId(goodsId);
    }

    /**
     * 根据商品id 查询
     *
     * @param commentId commentId
     * @return return
     */
    @Override
    public AjaxResult queryCommentWebsiteByCommentId(Long commentId) {
        SjdWebSiteGoodsCommentQueryResponseDTO responseDTO = commentMapper.getCommentWebSiteByCommentId(commentId);
        return AjaxResult.success(responseDTO);
    }

    /**
     * 删除评论
     *
     * @param commentId commentId
     * @return return
     */
    @Override
    public AjaxResult delComment(Long commentId) {
        int i = commentMapper.logicDeleteComment(commentId);
        if (i > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error("删除失败，请重试");
    }
}
